//app.js
App({

  onLaunch: function () {
    this.globalData.token = wx.getStorageSync('token') || ''
    this.globalData.is_login = this.globalData.token ? true : false

    if (this.globalData.is_login) {
      // 验证token
      wx.request({
        url: this.globalData.qiaofinn + 'member/token/verify/',
        data: {
          token: this.globalData.token
        },
        method: 'post',
        complete: (res) => {
          console.log(res)
          if (res.statusCode === 200) {
            wx.setStorageSync('token', res.data.token)
            this.globalData.token = res.data.token
            this.globalData.is_login = true
            this.globalData.identity_worker = res.data.user.member.identity === 2 ? true : false
          } else {
            wx.setStorageSync('token', '')
            this.globalData.token = ''
            this.globalData.is_login = false
          }
        }
      })
    }
    // 获取系统参数
    wx.getSystemInfo({
      success: e => {
        this.system = e
      }
    })
    // // 登录
    // wx.login({
    //   success: res => {
    //     // 发送 res.code 到后台换取 openId, sessionKey, unionId
    //   }
    // })

    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            lang: "zh_CN",
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              // wx.setStorageSync('userInfo', res.userInfo)
              this.globalData.userInfo = res.userInfo
              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })
    // 获取系统信息
    wx.getSystemInfo({
      success: e => {
        this.globalData.StatusBar = e.statusBarHeight;  //状态栏的高度，单位px
        let capsule = wx.getMenuButtonBoundingClientRect();    //获取菜单按钮（右上角胶囊按钮）的布局位置信息。坐标信息以屏幕左上角为原点
        if (capsule) {
          this.globalData.Custom = capsule;
          this.globalData.CustomBar = capsule.bottom + capsule.top - e.statusBarHeight;
        } else {
          this.globalData.CustomBar = e.statusBarHeight + 50;
        }
      }
    })
  },
  system: {},
  // 引入`towxml3.0`解析方法
  towxml: require('./towxml/index'),
  globalData: {
    userInfo: null,
    identity_worker: false,
    token: '',
    qqmapsdk_key: 'LR4BZ-LBTKU-A7VVG-2TVAW-SXWTV-PLBK2',
    qiaofinn:'https://qiaofinn.com/',
    // qiaofinn: 'http://192.168.2.165:8000/',
  },
  // request请求
  post: function (url, data, cb) {
    let header = { 'Content-Type': 'application/x-www-form-urlencoded' }
    if (this.globalData.token) {
      header = { 'Authorization': 'JWT ' + this.globalData.token }
    }
    wx.request({
      url: this.globalData.qiaofinn + url,
      data: data,
      method: 'post',
      header: header,
      complete: (res) => {
        if (res.statusCode === 401) {
          // 没有权限
          wx.showModal({
            title: '提示',
            content: '你还未登录，请先登录！',
            success(res) {
              if (res.confirm) {
                wx.navigateTo({
                  url: '/pages/mine/login/login',
                })
              }
            }
          })
          return
        }
        else if (res.statusCode === 404) {
          wx.showToast({
            title: '网络错误，请重新打开。',
            duration: 2500,
            icon: 'none'
          })
          return
        } else if (res.statusCode === 400) {
          wx.showToast({
            title: res.data.message,
            duration: 2500,
            icon: 'none'
          })
          return
        }
        else {
          return typeof cb == "function" && cb(res.data)
        }
      }
    })
  },
  get: function (url, data, cb, is_header = true) {
    let header = {}
    if (is_header && this.globalData.token) {
      header = { 'Authorization': 'JWT ' + this.globalData.token }
    }
    wx.request({
      url: this.globalData.qiaofinn + url,
      data: data,
      method: 'get',
      header: header,
      complete: (res) => {
        if (res.statusCode === 401) {
          // 没有权限
          wx.showModal({
            title: '提示',
            content: '你还未登录，请先登录！',
            success(res) {
              if (res.confirm) {
                wx.navigateTo({
                  url: '/pages/mine/login/login',
                })
              }
            }
          })
          return
        }
        if (res.statusCode !== 404) {
          return typeof cb == "function" && cb(res.data)
        } else {
          wx.showToast({
            title: '网络错误，请重启小程序',
            duration: 2500,
            icon: 'none'
          })
        }
      }
    })
  },
  uploadFile: function (filePath, url, cb) {
    var header = {
      'Authorization': 'JWT ' + this.globalData.token
    }
    wx.uploadFile({
      url: this.globalData.qiaofinn + url,
      filePath: filePath,
      name: 'file',
      header: header,
      // formData: {
      //   'path': path
      // },
      success(res) {
        if (res.statusCode === 200) {
          return typeof cb == "function" && cb(res.data)
        } else {
          wx.showModal({
            title: '提示',
            content: '网络错误，请重启小程序',
            showCancel: false
          })
        }
      }
    })
  },
})